package com.szholly.plug.arcgis.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.plug.arcgis.entity.GisConfigLodsRow;
import com.szholly.plug.arcgis.entity.GisConfigLodsTable;
import com.szholly.plug.arcgis.entity.GisConfigRow;
import com.szholly.plug.arcgis.entity.GisConfigTable;
import com.szholly.plug.arcgis.entity.GisLayerRow;
import com.szholly.plug.arcgis.entity.GisLayerTable;
import com.szholly.utils.freemarker.directive.DirectiveUtils;
import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.spring.springmvc.route.FunctionInfo;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.QueryMap;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * ArcGIS配置信息
 */
@Component("directive_gisconfig")
public class GisConfigDirective implements TemplateDirectiveModel, IDirective {
	
	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "tableName：数据字典表名 <br/>order：排序字段<br/>jbs：需要加载的级别"
				+ "调用示例：<br/>"
				+ "<@gisconfig><br/>"
				+ "<#if data??> <br/>" + "<#list data as item><br/>"
				+ "<li>${item.C_ORGNAME}</li><br/>" + "</#list><br/>"
				+ "</#if><br/>" + "</@gisconfig><br/>";

		FunctionInfo info = new FunctionInfo("treedata", 1, "表单",
				"获取树型下拉框的字典数据", paraInfo);
		return info;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] model,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		GisConfigRow config = GisConfigTable.getSingleRef().getEntity(
				GisConfigRow.class, new QueryMap());
		if (config == null) {

		} else {
			Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
					params);
			paramWrap.put("locator", DEFAULT_WRAPPER.wrap(config.getLocator()));
			paramWrap.put("geometryService", DEFAULT_WRAPPER.wrap(config.getGeometryServer()));
			paramWrap.put("printingServer", DEFAULT_WRAPPER.wrap(config.getPrintingServer()));
			
			paramWrap.put("rlocator", DEFAULT_WRAPPER.wrap(config.getRLocator()));
			String center = "[" + config.getDCenterx() + "," + config.getDCentery() + "]";
			paramWrap.put("center", DEFAULT_WRAPPER.wrap(center));
			
			// 地图级别
			QueryMap queryMap = new QueryMap();
			queryMap.getOrderByMap().put(GisConfigLodsRow.FIELD_ILevel, 1);
			List<GisConfigLodsRow> lods = GisConfigLodsTable.
					getSingleRef().getEntityList(GisConfigLodsRow.class, queryMap);
			String lodsString = "";
			String sliderLabels = "";
			for(GisConfigLodsRow lod : lods){
				lodsString += "{\"level\":"+lod.getILevel()
						+",\"resolution\":"+lod.getResolution()
						+",\"scale\":"+lod.getScale()+"},";
				
				sliderLabels += "'" + lod.getSTitle() + "',";
			}
			if(!StringUtils.IsNullOrSpace(lodsString)){
				lodsString = lodsString.substring(0, lodsString.length()-1);
				lodsString = "["+lodsString+"]";
				paramWrap.put("lods", DEFAULT_WRAPPER.wrap(lodsString));
				
				sliderLabels = sliderLabels.substring(0, sliderLabels.length()-1);
				sliderLabels = "["+sliderLabels+"]";
				paramWrap.put("sliderlabels", DEFAULT_WRAPPER.wrap(sliderLabels));
			}
			
			// 图层信息
			queryMap = new QueryMap();
			queryMap.getOrderByMap().put(GisLayerRow.FIELD_IIndex, "0");
			queryMap.getWhereMap().put(GisLayerRow.FIELD_ISUSE, "是");
			List<GisLayerRow> layers = GisLayerTable.getSingleRef().
					getEntityList(GisLayerRow.class, queryMap);
			String result = "";
			if(layers!=null && layers.size()>0){
				for(GisLayerRow layer : layers){
					String layerName = layer.getSLayername();
					String layerAliasName = layer.getSName();
					String groupName = layer.getSGroupname();
					String type = layer.getSType();
					String url = layer.getSUrl();
					String visible = layer.getSVisible();
					String showids = layer.getSShowids();
					
					result += "{name:'"+layerName+"',aliasName:'"+layerAliasName+"',group:'"
							+groupName +"',mtype:'"+type+"',url:'"+url+"',visible:'"
							+visible+"',showids:'"+showids+"'},";
				}
				if(result!=""){
					result = result.substring(0, result.length()-1);
					result = "["+result+"]";
				}
			}
			paramWrap.put("layers", DEFAULT_WRAPPER.wrap(result));
			paramWrap.put("zoom", DEFAULT_WRAPPER.wrap(config.getIInilevel()));
			paramWrap.put("maxzoom", DEFAULT_WRAPPER.wrap(config.getMaxLevel()));
			paramWrap.put("minzoom", DEFAULT_WRAPPER.wrap(config.getMinLevel()));
			paramWrap.put("initextent", DEFAULT_WRAPPER.wrap(config.getSIniextent()));
			paramWrap.put("dlwkid", DEFAULT_WRAPPER.wrap(config.getDLWkid()));
			paramWrap.put("tywkid", DEFAULT_WRAPPER.wrap(config.getTYWkid()));
			paramWrap.put("mapisdlwkid", DEFAULT_WRAPPER.wrap(config.getMapIsDlWkid()));
			
			Map<String, TemplateModel> origMap = DirectiveUtils
					.addParamsToVariable(env, paramWrap);
			body.render(env.getOut());
			DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
		}
	}
}
